//
//  ViewController.m
//  GCD
//
//  Created by user on 16/4/14.
//  Copyright © 2016年 user. All rights reserved.
//

#import "ViewController.h"
#import "SingleCase.h"
@interface ViewController ()
@property(nonatomic,strong)UIImage *image1;
@property(nonatomic,strong)UIImage *image2;

@property(nonatomic,strong)dispatch_source_t timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self timers];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self apply];
    
}
/**
 *threadgroup 异步＋线程组
 */
-(void)asyncGroup
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //创建一个队列组
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        
        NSURL *url= [NSURL URLWithString:@""];
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        self.image1 = [UIImage imageWithData:data];
        
        
    });
    dispatch_group_async(group, queue, ^{
        NSURL *url= [NSURL URLWithString:@""];
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        self.image2 = [UIImage imageWithData:data];
    });
    //组中所有队列执行完成
    dispatch_group_notify(group, queue, ^{
        
        UIGraphicsBeginImageContext(CGSizeMake(200, 200));
        
        [self.image1 drawInRect:CGRectMake(0, 0, 100, 100)];
        [self.image2 drawInRect:CGRectMake(100, 0, 100, 100)];
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //加载ui
        });
        
        
        
    });
    
}
/**
 * barri 阻赛 全局队列无效
 */
-(void)asnyBarri
{
    
    dispatch_queue_t queue = dispatch_queue_create("com.barrier.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        
        NSLog(@"1%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(queue, ^{
        
        NSLog(@"2%@",[NSThread currentThread]);
        
    });
    dispatch_barrier_async(queue, ^{
        
    });
    dispatch_async(queue, ^{
        
        NSLog(@"3----%@",[NSThread currentThread]);
        
    });
    

    
}
/**
 * 执行一次
 */
- (void)onceToken
{
    static dispatch_once_t  token;
    dispatch_once(&token, ^{
        
    });
}
/**
 *快速迭代
 */
-(void)apply
{
    dispatch_queue_t queue = dispatch_queue_create("com.apply.queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_apply(10, queue, ^(size_t index) {
        NSLog(@"%zd------%@",index,[NSThread currentThread]);
    });
    
}
/**
 * 同步函数＋主队列 发生阻赛
 */
- (void)syncMain
{
    //主队列 为 serial 队列
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        
        NSLog(@"1%@",[NSThread currentThread]);
        
    });
    
    dispatch_sync(queue, ^{
        
        NSLog(@"2%@",[NSThread currentThread]);
        
    });
    dispatch_sync(queue, ^{
        
        NSLog(@"3----%@",[NSThread currentThread]);
        
    });

}
/*异步函数＋主队列  只在主队列执行*/
-(void)asyncMain
{
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        
        NSLog(@"1%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(queue, ^{

            NSLog(@"2%@",[NSThread currentThread]);
        
    });
    dispatch_async(queue, ^{
      
            NSLog(@"3----%@",[NSThread currentThread]);
        
    });

    
}
/*异步函数 ＋ 串行队列*/
-(void)asyncSerial
{
    
    dispatch_queue_t queue = dispatch_queue_create("com.serial.queue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{

            NSLog(@"1%@",[NSThread currentThread]);
        
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"2%@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"3----%@",[NSThread currentThread]);
        }
    });

}
/* 并发队列 异步函数*/
- (void)asycConCureent
{
    //1.创建一个并发队列
//    dispatch_queue_t queue = dispatch_queue_create("com.conConcurrent.queue", DISPATCH_QUEUE_CONCURRENT);
    //获取全局队列 为并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"1------%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_async(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"2------%zd---%@",i,[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"3------%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    
}
/*
 *同步函数＋并发 不会开启新线程
 */

/* 并发队列 异步函数*/
- (void)syncConCureent
{
    //1.创建一个并发队列
    //    dispatch_queue_t queue = dispatch_queue_create("com.conConcurrent.queue", DISPATCH_QUEUE_CONCURRENT);
    //获取全局队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_sync(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"1------%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    dispatch_sync(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"2------%zd---%@",i,[NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (NSInteger i=0; i<10; i++) {
            NSLog(@"3------%zd---%@",i,[NSThread currentThread]);
        }
    });
    
    
}
//定时器
int count =0;
-(void)timers{
    
    
    //创建一个定时器
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
    //设置定时器的各种属性（几时开始，每隔多秒执行一次）
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW,(uint64_t)( 3*NSEC_PER_SEC));
    dispatch_source_set_timer(self.timer, start, 2*NSEC_PER_SEC, 0);
    //设置回调
    dispatch_source_set_event_handler(self.timer, ^{
        NSLog(@"---%@",[NSThread currentThread]);
        count++;
        if (count==4) {
            // 暂停定时器
            dispatch_cancel(self.timer);
            self.timer = nil;
        }
    });
    //恢复定时器
    dispatch_resume(self.timer);
    
 
    
}
@end
